//
//  QAddSpecialVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/2/19.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QAddSpecialVC.h"
#import "QInfoManageVC.h"


@interface QAddSpecialVC ()<UIWebViewDelegate>
{
    NSString * _Urlstr;
}
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation QAddSpecialVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.state == nil) {
        _Urlstr = @"addInvoice.html";
        self.title = @"添加发票" ;//专票

    }else if ([self.state isEqualToString:@"0"]){
        _Urlstr = @"lookInvoice.html";
        self.title = @"发票详情";//专票

    }else{
        _Urlstr = @"lookInvoice.html";
        self.title = @"发票详情";//专票

    }


    if (_url.length>0) {
        _Urlstr = _url;
    }
    
    
    [self.view addSubview:self.webView];
    [self requestData];
    
    
}

- (void)dealloc{
    NSLog(@"释放了");
}

- (void)requestData{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@webPage/appPage/%@",WEB_BASE_URL,_Urlstr]]];
    [_webView loadRequest:request];

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {


    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.context.exceptionHandler = // 打印异常
    ^(JSContext *context, JSValue *exceptionValue)
    {
        context.exception = exceptionValue;
        CLog(@"%@", exceptionValue);
    };

    __weak QAddSpecialVC *wSelf = self;
    
    self.context[@"getToken1"] =^(){
        CLog(@"完成===%@",[QUserInfo sharedInfo].loginModel.token);
        return [QUserInfo sharedInfo].loginModel.token;
    };
    self.context[@"getState1"] =^(){
        __strong QAddSpecialVC *sSelf = wSelf;
        
        NSLog(@"完成");
        if (sSelf.state == nil) {
            return @"-1";
        }else{
            return sSelf.state;
        }
    };
    self.context[@"getCredentialsId1"] =^(){
        __strong QAddSpecialVC *sSelf = wSelf;
        if (self.credentialsId == nil) {
            return @"-1";
        }else{
            return sSelf.credentialsId;
        }
        
    };

}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [QViewUtility hideMBHudToView:self.view];
    self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.context.exceptionHandler = // 打印异常
    ^(JSContext *context, JSValue *exceptionValue)
    {
        context.exception = exceptionValue;
        CLog(@"%@", exceptionValue);
    };
    self.context[@"getToken1"] =^(){
        CLog(@"完成===%@",[QUserInfo sharedInfo].loginModel.token);
        return [QUserInfo sharedInfo].loginModel.token;
    };
    
    __weak QAddSpecialVC *wSelf = self;

    self.context[@"finishThis"] =^(){
        __strong QAddSpecialVC *sSelf = wSelf;
        NSLog(@"完成");
        [sSelf finishThis];
    };
}
//token
- (NSString *)getToken1{
    
    return [QUserInfo sharedInfo].loginModel.token;
}
//状态
-(NSString *)getState1{
    
    return self.state;

}
//提交完成
- (void)finishThis{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        for (UIViewController *controller in self.navigationController.viewControllers){
            if ([controller isKindOfClass:[QInfoManageVC class]]){
                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationAddInvoiceSuccess object:nil];
                
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    });
}
//获取资质
- (NSString *)getCredentialsId1{
    if (self.credentialsId == nil) {
        return @"-1";
    }else{
        return self.credentialsId;
    }
    
}
#pragma mark - Getter/Setter
- (UIWebView*)webView {
    if (!_webView) {
        _webView = [UIWebView new];
        _webView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        _webView.delegate = self;
        _webView.scalesPageToFit = YES;
    }
    return _webView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
